package Dinamica;

public class Poligono {
	public static double f(int i, int j, int[] x, int[] y, int z)
			throws Throwable {

		int n = Math.abs(i - j);
		if (n <= 3) {
			return 0;
		} else {
			double res = Double.MAX_VALUE;
			for (int k = i; k < i + n; k++) {
				for (int l = i; l < i + n; l++) {
					int k1 = k % x.length;
					int l1 = l % x.length;
					if (k1 != l1 && (k1 + 1) % y.length != l1
							&& ((k1 + y.length - 1) % y.length) != l1) {
						double f1 = f(Math.min(k1, l1), Math.max(k1, l1), x, y,
								z + 1);
						double f2 = f(Math.max(k1, l1), Math.min(l1, k1), x, y,
								z + 1);
						res = Math.min(dis(x[k1], y[k1], x[l1], y[l1]) + f1
								+ f2, res);

					}
				}
			}
			return res;
		}
	}

	public static double dis(int x1, int y1, int x2, int y2) {
		return Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
	}

	public static void main(String[] args) throws Throwable {
		int x[] = { 3, 6, 9, 11, 6 };
		int y[] = { 6, 8, 8, 3, 0 };
		System.out.println(f(0, x.length - 1, x, y, 0));

	}
}
